package com.ec.mn.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ec.mn.entity.HealthRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 健康数据记录 Mapper 接口
 */
@Mapper
public interface HealthRecordMapper extends BaseMapper<HealthRecord> {
    
    /**
     * 根据老人用户ID查询健康记录
     */
    @Select("SELECT * FROM health_records WHERE elderly_user_id = #{elderlyUserId} ORDER BY recorded_at DESC")
    List<HealthRecord> selectByElderlyUserId(@Param("elderlyUserId") Long elderlyUserId);
    
    /**
     * 查询异常健康记录
     */
    @Select("SELECT * FROM health_records WHERE is_abnormal = 1 ORDER BY recorded_at DESC")
    List<HealthRecord> selectAbnormalRecords();
    
    /**
     * 根据老人用户ID查询异常健康记录
     */
    @Select("SELECT * FROM health_records WHERE elderly_user_id = #{elderlyUserId} AND is_abnormal = 1 ORDER BY recorded_at DESC")
    List<HealthRecord> selectAbnormalRecordsByElderlyUserId(@Param("elderlyUserId") Long elderlyUserId);
    
    /**
     * 根据数据类型查询健康记录
     */
    @Select("SELECT * FROM health_records WHERE record_type = #{recordType} ORDER BY recorded_at DESC")
    List<HealthRecord> selectByRecordType(@Param("recordType") String recordType);
    
    /**
     * 根据老人用户ID和数据类型查询健康记录
     */
    @Select("SELECT * FROM health_records WHERE elderly_user_id = #{elderlyUserId} AND record_type = #{recordType} ORDER BY recorded_at DESC")
    List<HealthRecord> selectByElderlyUserIdAndRecordType(@Param("elderlyUserId") Long elderlyUserId, @Param("recordType") String recordType);
    
    /**
     * 查询指定时间范围内的健康记录
     */
    @Select("SELECT * FROM health_records WHERE recorded_at BETWEEN #{startTime} AND #{endTime} ORDER BY recorded_at DESC")
    List<HealthRecord> selectByTimeRange(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
    
    /**
     * 根据老人用户ID查询指定时间范围内的健康记录
     */
    @Select("SELECT * FROM health_records WHERE elderly_user_id = #{elderlyUserId} AND recorded_at BETWEEN #{startTime} AND #{endTime} ORDER BY recorded_at DESC")
    List<HealthRecord> selectByElderlyUserIdAndTimeRange(@Param("elderlyUserId") Long elderlyUserId, 
                                                         @Param("startTime") LocalDateTime startTime, 
                                                         @Param("endTime") LocalDateTime endTime);
    
    /**
     * 统计老人用户的健康记录数量
     */
    @Select("SELECT COUNT(*) FROM health_records WHERE elderly_user_id = #{elderlyUserId}")
    Long countByElderlyUserId(@Param("elderlyUserId") Long elderlyUserId);
    
    /**
     * 统计异常健康记录数量
     */
    @Select("SELECT COUNT(*) FROM health_records WHERE is_abnormal = 1")
    Long countAbnormalRecords();
    
    /**
     * 统计指定老人用户的异常健康记录数量
     */
    @Select("SELECT COUNT(*) FROM health_records WHERE elderly_user_id = #{elderlyUserId} AND is_abnormal = 1")
    Long countAbnormalRecordsByElderlyUserId(@Param("elderlyUserId") Long elderlyUserId);
    
    /**
     * 分页查询健康记录（自定义分页）
     */
    @Select("SELECT * FROM health_records ORDER BY recorded_at DESC")
    IPage<HealthRecord> selectPageWithCustom(Page<HealthRecord> page);
    
    /**
     * 根据老人用户ID分页查询健康记录
     */
    @Select("SELECT * FROM health_records WHERE elderly_user_id = #{elderlyUserId} ORDER BY recorded_at DESC")
    IPage<HealthRecord> selectPageByElderlyUserId(Page<HealthRecord> page, @Param("elderlyUserId") Long elderlyUserId);
    
    /**
     * 查询最近7天的健康记录
     */
    @Select("SELECT * FROM health_records WHERE recorded_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) ORDER BY recorded_at DESC")
    List<HealthRecord> selectRecentRecords();
    
    /**
     * 根据老人用户ID查询最近7天的健康记录
     */
    @Select("SELECT * FROM health_records WHERE elderly_user_id = #{elderlyUserId} AND recorded_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) ORDER BY recorded_at DESC")
    List<HealthRecord> selectRecentRecordsByElderlyUserId(@Param("elderlyUserId") Long elderlyUserId);
} 